package com.bwie.redkids.fragment;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.http.client.ClientProtocolException;

import com.bwie.redkids.FlashSaleActivity;
import com.bwie.redkids.HotSaleActivity;
import com.bwie.redkids.NewProductActivity;
import com.bwie.redkids.R;
import com.bwie.redkids.RecommendActivity;
import com.bwie.redkids.SalesPromotionActivity;
import com.bwie.redkids.adapter.HomeListBaseAdapter;
import com.bwie.redkids.adapter.HomePagerAdapter;
import com.bwie.redkids.bean.HomeListVieweBean;
import com.bwie.redkids.bean.HomeViewPagerBean;
import com.bwie.redkids.bean.HomeViewPagerBean.MyHomeBanner;
import com.bwie.redkids.utils.GsonUtils;
import com.bwie.redkids.utils.PathUtils;
import com.google.gson.Gson;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;

public class HomeFragment extends Fragment {
	private ArrayList<HomeListVieweBean> arrayList;
	private ViewPager home_viewPager;
	private ListView home_listView;
	private int SUCESS = 0;
	private ArrayList<MyHomeBanner> homeList = new ArrayList<HomeViewPagerBean.MyHomeBanner>();
	private ArrayList<ImageView> pointList;
	private LinearLayout ll_point;
	private FragmentActivity activity;

	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (msg.what == SUCESS) {
				HomeViewPagerBean homeBean = (HomeViewPagerBean) msg.obj;
				System.out
						.println("----------------------homeBean-----------------"
								+ homeBean.toString());
				homeList.addAll(homeBean.getHome_banner());
				home_viewPager.setAdapter(new HomePagerAdapter(activity,
						homeList));
				initDots();
				home_viewPager.setCurrentItem(10000 * homeList.size());
			}
		};
	};
	private ImageView home_search;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.home_fragment, null, false);
		activity = getActivity();
		initView(view);
		return view;
	}

	private void initView(View view) {
		home_search = (ImageView) view.findViewById(R.id.home_search);
		home_viewPager = (ViewPager) view.findViewById(R.id.home_viewPager);
		home_listView = (ListView) view.findViewById(R.id.home_listView);
		ll_point = (LinearLayout) view.findViewById(R.id.ll_point);
		arrayList = new ArrayList<HomeListVieweBean>();
		arrayList
				.add(new HomeListVieweBean(R.drawable.home_classify_01, "限时抢购"));
		arrayList
				.add(new HomeListVieweBean(R.drawable.home_classify_02, "促销快报"));
		arrayList
				.add(new HomeListVieweBean(R.drawable.home_classify_03, "新品上架"));
		arrayList
				.add(new HomeListVieweBean(R.drawable.home_classify_04, "热门单品"));
		arrayList
				.add(new HomeListVieweBean(R.drawable.home_classify_05, "推荐品牌"));
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		getViewPagerInformation();

		setListAdapter();
		Timer timer = new Timer();
		TimerTask task = new TimerTask() {

			@Override
			public void run() {
				activity.runOnUiThread(new Runnable() {

					@Override
					public void run() {
						home_viewPager.setCurrentItem(home_viewPager
								.getCurrentItem() + 1);
					}
				});
			}
		};
		timer.schedule(task, 3000, 3000);

		home_viewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				for (int i = 0; i < homeList.size(); i++) {
					if (i == position % homeList.size()) {
						pointList.get(i).setImageResource(
								R.drawable.slide_adv_selected);
					} else {
						pointList.get(i).setImageResource(
								R.drawable.slide_adv_normal);
					}
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});
	}

	private void setListAdapter() {

		home_listView.setAdapter(new HomeListBaseAdapter(getActivity(),
				arrayList));

		home_listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				if (position == 0) {
					Intent intent = new Intent(activity,
							FlashSaleActivity.class);
					startActivity(intent);
				} else if (position == 1) {
					Intent intent = new Intent(activity,
							SalesPromotionActivity.class);
					startActivity(intent);
				} else if (position == 2) {
					Intent intent = new Intent(activity,
							NewProductActivity.class);
					startActivity(intent);
				} else if (position == 3) {
					Intent intent = new Intent(activity, HotSaleActivity.class);
					startActivity(intent);
				} else if (position == 4) {
					Intent intent = new Intent(activity,
							RecommendActivity.class);
					startActivity(intent);
				}

			}
		});
	}

	private void initDots() {
		pointList = new ArrayList<ImageView>();
		pointList.clear();
		ll_point.removeAllViews();
		for (int i = 0; i < homeList.size(); i++) {
			ImageView imageView = new ImageView(activity);
			// imageView.setPadding(10, 10, 10, 10);
			Drawable drawable = null;
			if (i == 0) {
				// ÁÁÉ«Í¼Æ¬
				drawable = getResources().getDrawable(
						R.drawable.slide_adv_selected);

			} else {
				drawable = getResources().getDrawable(
						R.drawable.slide_adv_normal);
			}
			imageView.setImageDrawable(drawable);
			LayoutParams params = new LayoutParams(dip2px(activity, 10),
					dip2px(activity, 10));
			params.setMargins(dip2px(activity, 5), 0, dip2px(activity, 5), 0);
			ll_point.addView(imageView, params);
			pointList.add(imageView);

		}
	}

	public int dip2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	private void getViewPagerInformation() {
		new Thread() {
			public void run() {
				try {
					String data = GsonUtils.getData(PathUtils.PATH
							+ PathUtils.HOME_VIEWPAGER);
					Gson gson = new Gson();
					HomeViewPagerBean fromJson = gson.fromJson(data,
							HomeViewPagerBean.class);

					handler.obtainMessage(SUCESS, fromJson).sendToTarget();
				} catch (ClientProtocolException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			};
		}.start();
	}
}
